import os,tempfile,shutil,commands
import pickle
import sys
import time


class CRVClientConfig:
    def __init__(self):
        self.cloudcrvsrv=""
        self.statusurl=""
        self.vmid=""
        self.certname=""
        self.curlpem=""
        self.cacertpem=""
        self.workdir=""
        self.keypass=""
        self.curlopt=""

        self.puppetservercertname="puppetsrv"
        self.puppetserver=""
        
        self.puppetcertname=""
        self.puppetvardir="/var/puppet/varpuppet"
        self.puppetssldir=""
        self.puppetpassfile=""

class CRVClientTools:
    def __init__(self, config):
        self.__config=config

    def runPuppet(self, logfile=None):
        starttime=time.time()
        cmdstr='puppetd --test --debug --server=%s --certname=%s --vardir=%s --ssldir=%s --passfile=%s --trace'%(self.__config.puppetservercertname, self.__config.puppetcertname, self.__config.puppetvardir, self.__config.puppetssldir, self.__config.puppetpassfile)
        (status,output)=commands.getstatusoutput(cmdstr)
        endtime=time.time()

        logstr="Command=%s, status=%d, output=%s\n"%(cmdstr,status,output)+"\nStart time=%f, end time=%f\n"%(starttime,endtime)
        
        if logfile is None:
            print logstr
        
        else:
            ouf=open(logfile,"a")
            ouf.write(logstr)
            ouf.close()
        
        error=False
        warn=False
        if output.find("err:")>=0:
            error=True
        if output.find("warn:")>=0:
            warn=True

        return (error,warn)

    def doSecureStatusCurlUntilDone(self, url, exitonoutput=None, waittime=2):

        #repeat calling curl until a successfully curl call
        #successful curl needs all below true:
        # 1. return status=0
        # 2. if exitonoutput=None, no more constrains (true)
        #    otherwise when returned output=exitonoutput

        while True:
            (status,output)=self.doSecureStatusCurl(url)
            if status==0:
                if exitonoutput is None:
                    return output
                else:
                    if output==exitonoutput:
                        return output
                        
            time.sleep(waittime)

    def doSecureStatusCurl(self, url):
        cmdstr="curl -s %s \"%s/%s\""%(self.__config.curlopt, self.__config.statusurl,url)
        (status,output)=commands.getstatusoutput(cmdstr)
        print "doSecureCurl: Running command=%s, status=%d, output=%s"%(cmdstr,status,output)
        return (status, output)

    
    def doGenericCurl(self,fullurl):
        cmdstr="curl -s %s"%fullurl
        (status,output)=commands.getstatusoutput(cmdstr)
        print "doGenericCurl: Running command=%s, status=%d, output=%s"%(cmdstr,status,output)
        return (status, output)
        
    
    def doGenericCurlUntilDone(self,fullurl, exitonoutput=None, waittime=2):

        #repeat calling curl until a successfully curl call
        #successful curl needs all below true:
        # 1. return status=0
        # 2. if exitonoutput=None, no more constrains (true)
        #    otherwise when returned output=exitonoutput

        while True:
            (status,output)=self.doGenericCurl(fullurl)
            if status==0:
                if exitonoutput is None:
                    return output
                else:
                    if output==exitonoutput:
                        return output
                        
            time.sleep(waittime)
        
        
    
